package n3250;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		BufferedReader  bf= new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(bf.readLine());
		int a[]=new int[N];
		int top=-1;
		long sum=0;
		for(int i=0;i<N;i++)
		{
			int cur=Integer.parseInt(bf.readLine());
			while(top>=0&&a[top]<=cur)
			{
				top--;
			}
			a[++top]=cur;
			sum+=top;
		}
		System.out.println(sum);
	}

}
